/*
 * Copyright (c) 2005, 2014 vacoor
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 */
package org.vacoor.nothing.common.misc;

/**
 * 河南联通宽带帐号加密算法
 * <p/>
 * 将河南联通宽带帐号转换为PPPoE帐号
 *
 * @author vacoor
 * @version 1.0
 */
public class HaCNCEncoder {
    /**
     * 将宽带帐号转换为PPPoE帐号
     *
     * @param input 数字宽带帐号
     * @return PPPoE帐号
     */
    public static String encode(String input) {
        String result = "";
        String dictionary = "9012345678abcdeABCDEFGHIJKLMNfghijklmnUVWXYZxyzuvwopqrstOPQRST";
        int[] mask = {17, 52, 201, 35, 117, 24, 215, 226, 18, 53, 41, 43, 236, 182, 35, 25};
        int cache = 37;
        int l;
        for (int i = 0; i < input.length(); i++) {
            for (int j = 0; j < dictionary.length(); j++) {
                if (input.charAt(i) == dictionary.charAt(j)) {
                    l = (((mask[i & 0xF]) ^ (3 * cache)) ^ (i * 5)) + j;
                    result += dictionary.charAt(l % dictionary.length());
                    cache = cache ^ (l % dictionary.length() + 9433);
                    break;
                }
            }
            if (result.charAt(i) == ' ') {
                //dictionary[i] = i;
                dictionary = dictionary.substring(0, i) + i + dictionary.substring(i, dictionary.length());
            }
        }
        return "2:" + result;
    }

    /**
     * 将PPPoE账号还原为宽带账号
     *
     * @param source
     * @return
     */
    public static String decode(String source) {
        String dics = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
        // source = source.startsWith("2:") ? source.substring(2,
        // source.length()) : source;
        //source = source.replace("2:", "");
        String result = "";
        for (int j = 0; j < source.length(); j++) {
            for (int i = 0; i < dics.length(); i++) {
                String tmp = result + dics.charAt(i);
                if (source.substring(0, j + 1).equals(encode(tmp))) {
                    result = tmp;
                    break;
                }
            }
        }
        return result;
    }

    /**
     * ****************************************
     */
    public static void main(String[] args) {
        // 0379 8个6
        //2:2v49tdIngbJq
        args = new String[]{"-encode", "037966666666"};
        if (2 != args.length) {
            displayHelp();
            System.exit(1);
        }
        if ("-encode".equals(args[0])) {
            System.out.println(encode(args[1]));
        } else if ("-decode".equals(args[0])) {
            System.out.println(decode(args[1]));
        } else {
            displayHelp();
            System.exit(1);
        }
    }

    public static void displayHelp() {
        System.out.println("Usage: java HaCNCEncoder option source");
        System.out.println("options:");
        System.out.println("   -encode: from Telephone to PPPoE");
        System.out.println("   -decode: from PPPoE to Telephone");
        System.out.println();
        System.out.println("java HaCNCEncoder -encode 037966666666");
        System.out.println("java HaCNCEncoder -decode 2:2v49tdIngbJq");
    }
}